package PriorityQueue;

import java.util.PriorityQueue;

public class BiggestK {
    public int[] biggestK(int[] array,int k){
        int[] ret = new int[k];
        if(array==null||k<=0){
            return ret;
        }
        PriorityQueue<Integer> priorityQueue = new PriorityQueue<>();//默认小根堆
        //优先级队列的问题，大多都是反过来建堆的，求大建小，求小建大
        for (int i = 0; i < k; i++) {
            priorityQueue.offer(array[i]);
        }
        for (int i = k; i < array.length; i++) {
            int top = priorityQueue.peek();
            if(array[i]>top){
                priorityQueue.poll();
                priorityQueue.offer(array[i]);
            }
        }
        for (int i = 0; i < k; i++) {
            ret[i]=priorityQueue.poll();
        }
        return ret;
    }
}
